package com.jinzhi.jzweb.controller.api;

import cn.hutool.crypto.asymmetric.Sign;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.jinzhi.common.utils.ApiResult;
import com.jinzhi.common.utils.Result;
import com.jinzhi.jzapi.domain.UserDO;
import com.jinzhi.jzapi.utils.JWTUtil;
import com.jinzhi.jzweb.domain.JazUserAuntDO;
import com.jinzhi.jzweb.domain.JazUserIntegralDO;
import com.jinzhi.jzweb.domain.JazUserSignInDO;
import com.jinzhi.jzweb.service.JazUserIntegralService;
import com.jinzhi.jzweb.service.JazUserSignInService;
import com.jinzhi.jzweb.utils.DateUtil;
import com.jinzhi.sys.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * @author mengzhichao
 * @create 2023-04-26-15:30
 */
@RestController
@RequestMapping("/jzApi/ApiUserIntegral")
public class ApiJazUserIntegral {

    private int integral = 5; //每次签到获取的积分数


    @Autowired
    private UserService userService;

    @Autowired
    private JazUserSignInService jazUserSignInService; //签到表


    @Autowired
    private JazUserIntegralService jazUserIntegralService; //积分明细表


    /**
     * 用户签到
     * @return
     */
    @PostMapping("/signIn")
    @Transactional
    public Result<?> signIn(){

        UserDO getUser = JWTUtil.getUser();
		UserDO userDO = userService.selectById(getUser.getId());

        Date start = DateUtil.strToDateLong(DateUtil.dateToStr(new Date(), Locale.CHINA) + " 00:00:00");
        Date end = DateUtil.strToDateLong(DateUtil.dateToStr(new Date(), Locale.CHINA) + " 23:59:59");
        EntityWrapper<JazUserSignInDO> wrapper = new EntityWrapper<JazUserSignInDO>();
        wrapper.eq("sys_userid",userDO.getId());
        wrapper.ge("sign_time",start);
        wrapper.lt("sign_time",end);

        List<JazUserSignInDO> integralUserSignIns = jazUserSignInService.selectList(wrapper);
        if (integralUserSignIns.size()>0){
            return Result.fail("今日已签到");
        }

        JazUserSignInDO jazUserSignInDO =new JazUserSignInDO();
        jazUserSignInDO.setSysUserid(userDO.getId());
        jazUserSignInDO.setSignIntegral(integral);
        jazUserSignInDO.setSignTime(new Date());


        JazUserIntegralDO jazUserIntegralDO = new JazUserIntegralDO();
        jazUserIntegralDO.setIntegralDetail("+"+integral); //积分
        jazUserIntegralDO.setSysUserid(userDO.getId()); //用户id
        jazUserIntegralDO.setIntegralTime(new Date()); //时间
        jazUserIntegralDO.setIntegralWay("每日签到"); //用途



        String userIntegral = userDO.getCurrentPoints();
        if (StringUtils.isEmpty(userIntegral)){
            userIntegral = "0";
        }
        userIntegral = String.valueOf(Double.valueOf(userIntegral) +Double.valueOf(integral));
        jazUserIntegralDO.setCurrentIntegral(userIntegral); //当前积分
        jazUserIntegralService.insert(jazUserIntegralDO);
        userDO.setCurrentPoints(userIntegral);
        userDO.setAccumulatedPoints(String.valueOf(Double.valueOf(userDO.getAccumulatedPoints()) + Double.valueOf(integral))); //累计积分
        userService.updateById(userDO);

        boolean save = jazUserSignInService.insert(jazUserSignInDO);
        return Result.ok(save);
    }





    /**
     * 查询用户是否签到
     * @return
     */
    @GetMapping("/getSignIn")
    public Result<?> getSignIn(){

        UserDO getUser = JWTUtil.getUser();
		UserDO userDO = userService.selectById(getUser.getId());
//        UserDO userDO = userService.selectById(1328881670693347330L);

        int signin =0; //0未签到 1已签到

        Date start = DateUtil.strToDateLong(DateUtil.dateToStr(new Date(), Locale.CHINA) + " 00:00:00");
        Date end = DateUtil.strToDateLong(DateUtil.dateToStr(new Date(), Locale.CHINA) + " 23:59:59");

        EntityWrapper<JazUserSignInDO> wrapper = new EntityWrapper<JazUserSignInDO>();
        wrapper.eq("sys_userid",userDO.getId());
        wrapper.ge("sign_time",start);
        wrapper.lt("sign_time",end);

        List<JazUserSignInDO> integralUserSignIns = jazUserSignInService.selectList(wrapper);
        if (integralUserSignIns.size()>0){
            signin = 1;
        }

        return Result.ok(signin);
    }




    /**
     * 用户个人积分明细查询
     * @return
     */
    @GetMapping("/getUserDetail")
    public Result<?> getUserDetail(String current, String size){

        UserDO getUser = JWTUtil.getUser();
		UserDO userDO = userService.selectById(getUser.getId());

        EntityWrapper<JazUserIntegralDO> wrapper = new EntityWrapper<JazUserIntegralDO>();
        wrapper.eq("sys_userid",userDO.getId());
        wrapper.orderBy("integral_time",false);

        List<JazUserIntegralDO> jazUserIntegralDOS = jazUserIntegralService.selectList(wrapper);

        Map map =new HashMap();
        map.put("userCurrentPoints",userDO.getCurrentPoints());//用户当前积分
        map.put("jazUserIntegralDOS",jazUserIntegralDOS);
        return Result.ok(map);
    }



















    /**
     * 邀请人接口
     * @return
     */
    @GetMapping("/inviter")
    public  ApiResult<?> inviter() {
        Map map =new HashMap();
        map.put("title","邀请好友入驻");
        map.put("desc","邀请好友赚积分");
        map.put("img","https://jinzhiweiye.oss-cn-hangzhou.aliyuncs.com/2023/314/invite.png");
        UserDO getUser = JWTUtil.getUser();
        UserDO userDO = userService.selectById(getUser.getId());
        map.put("inviter",userDO.getId()); //谁邀请的

        return ApiResult.success(map);
    }






}
